// pages/manager/manager.js
const app = getApp()
import apis from '../../config/index'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: true,
    productList: [],
    managerObj: {},
    phone: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.phone) {
      this.setData({
        phone: options.phone
      })
      wx.setStorageSync('phone', options.phone)
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getProductList()
    this.isManager()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: this.data.managerObj.name + '的名片',
      path: '/pages/manager/manager?phone=' + this.data.managerObj.phone,
      imageUrl: this.data.managerObj
    }
  },

  isManager() {
    const that = this
    wx.showLoading()
    app.getAccessToken(app.getPageUrl(getCurrentPages())).then(accessToken => {
      if (wx.getStorageSync('manager')) {
        const obj = wx.getStorageSync('manager')
        that.setData({
          managerObj: obj
        })
      } else {
        wx.request({
          url: app.globalData.hostAPI + apis.getPhone + '?access_token=' + accessToken,
          method: 'GET',
          data: {
            type: '2',
            phone: wx.getStorageSync('phone')
          },
          success: res => {
            if (res.data.status === 1) {
              that.setData({
                managerObj: res.data.data
              })
            } else if (res.data.status === -1) {
              wx.removeStorageSync('access_token')
              wx.navigateTo({
                url: '/pages/login/login',
              })
            } else {
              wx.showToast({
                title: res.data.msg
              })
            }
          }
        })
      }
      wx.hideLoading()
    })
  },

  addContract () {
    wx.addPhoneContact({
      firstName: this.data.managerObj.name,
      mobilePhoneNumber: this.data.managerObj.phone
    }).catch(e => {
      wx.showToast({
        title: '操作取消',
        icon: 'error'
      })
    })
  },

  getProductList () {
    const that = this
    wx.showLoading()
    wx.request({
      // url: app.globalData.hostAPI + apis.productList,
      url: app.globalData.hostAPI + apis.productList + '?attr_type=2',
      method: 'GET',
      success: res => {
        that.setData({
          productList: res.data.data
        })
        wx.hideLoading()
      }
    })
  },

  onCall () {
    wx.makePhoneCall({
      phoneNumber: this.data.managerObj.phone
    }).catch(e => {
      wx.showToast({
        title: '操作取消',
        icon: 'error'
      })
    })
  },
  onCopy(e) {
    wx.setClipboardData({
      data: e.currentTarget.dataset.text
    })
  },

  // 产品详情
  toDetail(e) {
    wx.navigateTo({
      url: '/pages/product/product-detail?gid=' + e.currentTarget.dataset.id,
    })
  },

  // 
  getPhoneNumber (e) {
    const that = this
    wx.showLoading()
    app.getAccessToken(app.getPageUrl(getCurrentPages())).then(function (accessToken) {
      wx.request({
        url: app.globalData.hostAPI + apis.getPhone + '?access_token=' + accessToken,
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          encryptedData: e.detail.encryptedData,
          iv: e.detail.iv
        },
        success: res => {
          if (res.data.status === 1) {
            if (res.data.data.name != '') {
              that.setData({
                managerObj: res.data.data
              })
              wx.setStorageSync('manager', that.data.managerObj)
              wx.setStorageSync('phone', that.data.managerObj.phone)
            }
          } else if (res.data.status === -1) {
            wx.removeStorageSync('access_token')
            wx.navigateTo({
              url: '/pages/login/login',
            })
          } else {
            wx.showToast({
              title: res.data.msg
            })
          }
          wx.hideLoading()
        }
      })
    })
  }
})